<?php
/**
 * 自定义表单
 */
namespace Common\Model;


class FormModel extends BaseModel{
	static $instance=null;
	public static function getFormInstance(){
		if(!self::$instance){
			self::$instance=M('form',null,DB_MAIN_CFG);
		}
		return self::$instance;
	}

	static $waveInstance=null;
	public static function getDataInstance(){
		if(!self::$waveInstance){
			self::$waveInstance=M('form_data',null,DB_MAIN_CFG);
		}
		return self::$waveInstance;
	}


	static $birthInstance=null;
	public static function getBirthInstance(){
		if(!self::$birthInstance){
			self::$birthInstance=M('tmp_babyinfo',null,DB_MAIN_CFG);
		}
		return self::$birthInstance;
	}



	//红包抽奖获得类型
	const FIELD_TEXT		= 0;//文字
	const FIELD_LONGTEXT	= 1;//文字
	const FIELD_NUM			= 2;//数字
	const FIELD_DATE_PAST	= 3;//日期(过去)
	const FIELD_TIME		= 4;//时间
	const FIELD_PHOTO		= 5;//图片
	const FIELD_SINGLE		= 6;//单选
	const FIELD_MULTI		= 7;//复选
	const FIELD_PHONE		= 8;//手机号
	const FIELD_EMAIL		= 9;//手机号
	const FIELD_AREA		= 11;//时间
	const FIELD_DATE_FUTURE	= 12;//日期(将来)


	public static function getFieldType(){
		return array(
			self::FIELD_TEXT		=>'文本',
			self::FIELD_LONGTEXT	=>'长文本',
			self::FIELD_NUM		=>'数值',
			self::FIELD_PHONE	=>'手机号',
			self::FIELD_DATE_PAST	=>'日期(过去)',
			self::FIELD_DATE_FUTURE	=>'日期(将来)',
			self::FIELD_TIME	=>'时间',
			self::FIELD_PHOTO	=>'图片',
			self::FIELD_SINGLE	=>'单选',
			self::FIELD_MULTI	=>'复选',
			self::FIELD_EMAIL	=>'邮箱',
			self::FIELD_AREA	=>'地区',

		);
	}


	//红包操作状态
	const PACK_STATUS_ALL			=0;//全部
	const PACK_STATUS_ONLINE   		=1;//正常
	const PACK_STATUS_PAUSE   		=2;//暂停，下线状态




	/**
	 * 获取红包细分记录
	 * @param $page
	 * @param $pageSize
	 * @param int $fid
	 * @return array
	 */
	public static function getRecordList($page,$pageSize,$fid=0,$nickname='',$beginTime=0,$endTime=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$tradeM = self::getDataInstance();
		$where='1';
		if ($fid>0){
			$where .=" and fid={$fid}";
		}
		if ($nickname>0){
			$where .=" and nickname like '%{$nickname}%'";
		}

		if($beginTime>0){
			$where .=" and addTime >={$beginTime}";
		}
		if($endTime>0){
			$where .=" and addTime <={$endTime}";
		}
		$dataSuccess['total'] = $tradeM->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
		$offset=($dataSuccess['page']-1)*$pageSize;
		$exchangeList = $tradeM->where($where)
			->order('id desc')
			->limit($offset,$pageSize)
			->select();
		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}


	public static function getBirthList($page,$pageSize,$month=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$tradeM = self::getBirthInstance();

		if ($month>0){
			$month=substr('0'.$month,-2);

			$tmp=$tradeM->field("uid,parentName,phone,address,babySex1,babyName1,babyBirthday1")
				->table('tmp_babyinfo')
				->where("babyBirthday1 like '%-{$month}-%'")
				->union("SELECT uid,parentName,phone,address,babySex2,babyName2,babyBirthday2 FROM `tmp_babyinfo` where babyBirthday2 like '%-{$month}-%'")
				->union("SELECT uid,parentName,phone,address,babySex3,babyName3,babyBirthday3 FROM `tmp_babyinfo` where babyBirthday3 like '%-{$month}-%'");

			$data=$tmp->select();


			$dataSuccess['total'] = count($data);//获取数据总条数
			$dataSuccess['page'] = $page ? $page : 1;//返回当前页
			$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
			$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
			$offset=($dataSuccess['page']-1)*$pageSize;
			$query=" select * from (select uid,parentName,phone,address,babySex1,babyName1,babyBirthday1 from tmp_babyinfo where babyBirthday1 like '%-{$month}-%'
 union SELECT uid,parentName,phone,address,babySex2,babyName2,babyBirthday2 FROM `tmp_babyinfo` where babyBirthday2 like '%-{$month}-%'
 union SELECT uid,parentName,phone,address,babySex3,babyName3,babyBirthday3 FROM `tmp_babyinfo` where babyBirthday3 like '%-{$month}-%'
 ) as a limit {$offset},{$pageSize} ";


			$dataSuccess['data']=$tradeM->query($query);
		}else{
			$tmp=$tradeM->field("uid,parentName,phone,address,babySex1,babyName1,babyBirthday1")
				->table('tmp_babyinfo')
				->union("SELECT uid,parentName,phone,address,babySex2,babyName2,babyBirthday2 FROM `tmp_babyinfo` where babyBirthday2 ")
				->union("SELECT uid,parentName,phone,address,babySex3,babyName3,babyBirthday3 FROM `tmp_babyinfo` where babyBirthday3 ");
			$data=$tmp->select();

			$dataSuccess['total'] = count($data);//获取数据总条数
			$dataSuccess['page'] = $page ? $page : 1;//返回当前页
			$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
			$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
			$offset=($dataSuccess['page']-1)*$pageSize;

			$query=" select * from (select uid,parentName,phone,address,babySex1,babyName1,babyBirthday1 from tmp_babyinfo where babyBirthday1 
 union SELECT uid,parentName,phone,address,babySex2,babyName2,babyBirthday2 FROM `tmp_babyinfo` where babyBirthday2 
 union SELECT uid,parentName,phone,address,babySex3,babyName3,babyBirthday3 FROM `tmp_babyinfo` where babyBirthday3 
 ) as a limit {$offset},{$pageSize} ";

			$dataSuccess['data']=$tradeM->query($query);
		}

		$uidstr='0';
		foreach ($dataSuccess['data'] as $one){
			$uidstr .= ','.$one['uid'];
		}
		$uk=UserModel::getInstance()->where("uid in ({$uidstr})")->getField('uid,nickname');

		foreach ($dataSuccess['data'] as $k=>$one){
			$dataSuccess['data'][$k]['nickname']=$uk[$one['uid']];
		}
		return $dataSuccess;

//
//
//		        $result = $this->query("select table_name from user_tables");
//        $info   =   array();
//        foreach ($result as $key => $val) {
//			$info[$key] = current($val);
//		}
//        return $info;
	}



	/**
	 * 获取红包活动列表数据的基础方法
	 * Author: shijy
	 * @param $packStatus 红包操作状态
	 * @param $page
	 * @param $pageSize
	 * @param int $uid 用户
	 * @return array
	 */
	public static function getList($page,$pageSize){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$pointMall = self::getFormInstance();
		$where='1';

		$dataSuccess['total'] = $pointMall->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是否有下一页
		$offset=($dataSuccess['page']-1)*$pageSize;

		$exchangeList = $pointMall->where($where)
			->order('addTime desc')
			->limit($offset,$pageSize)
			->select();

		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}


	/**
	 * 获取红包活动键值
	 * @return array
	 */
	public static function getFormKV(){
		$list=self::getFormInstance()->getField('fid,title');
		return $list;
	}



}